Ext.ns('TaxonTagger');
TaxonTagger.classficaionView = function(config){
	this.htw = new TaxonTagger.NameGrid({
			title: ''
		,	iconCls: ''	
		,	tools: []
		,	header: false
	});
	
	this.htw.on('urlLoaded', function(url){
		this.loadClassifaction(url);
		this.lasturlLoaded = url;
	}, this);
	
	this.tbar = new TaxonTagger.toolbar();
	this.htw.on('addNames', this.loadNames, this);
	
	this.htw.showChild();
	
	this.tbar = new TaxonTagger.toolbar();
	this.tbar.on('clearList', this.clearList, this);
	var tConfig = Ext.decode(localStorage.getItem("settings")) || Config;
	this.htw.primaryCombo.setValue(tConfig["general"]["primary"]);
	this.htw.secondaryCombo.setValue(tConfig["general"]["secondary"]);
	
	this.header = {
			height: 95
		,	cls: 'glossary-header'		
		,	region: 'north'
		,	xtype: 'box'
		,	border: false
		,	margins: '0 0 5 0'					
		,	html: '<div class="header">'+
				 '<a href="http://www.gbif.org/" target="_blank" class="gbif">'+
					 '<img src="resources/images/gbif.jpg" alt="GBif" width="104" height="90" border="0" /></a>'+
				 '<div class="header-text">'+
					 '<h1 id="headerId">Classification Service ( Source: '+ tConfig["general"]["nfServerName"] +' ) </h1>'+
					 '<p><i>A tool for mapping scientific names to selected classifications.</i></p>'+
				 '</div></div>'
	}
	var serviceName = Ext.decode(localStorage.getItem("settings")) || Config;
	var tConfig = Ext.decode(localStorage.getItem("settings")) || Config;
	Ext.apply(this, config, {
			id: 'viewport'
		,	layout: 'border'	
		,	lasturlLoaded: ''
		,	lastService: serviceName["general"]["nfServer"]
		,	items: [this.header, {
					region: 'center'
				,	border: false
				,	scope: this
				,	ref: 'centerPanel'
				,	layout: 'fit'
				,	tbar: this.tbar
				,	defaults: {
						border: false
					}
				,	items: [this.htw]
			}]
	});

	TaxonTagger.classficaionView.superclass.constructor.call(this, config);
	
};

Ext.extend(TaxonTagger.classficaionView, Ext.Viewport, {
		clearList: function(){
			Ext.Msg.confirm(this.txtRemoveTitle, this.txtRemoveMsg, function(btn){
				if(btn == 'yes'){
					TaxonTagger.docStore.removeAll();
					this.tbar.disableChild();
					this.tbar.clearEnableDisable();
					this.htw.lasturlLoaded = '';
					this.htw.lastClassified = '';
					this.lasturlLoaded = '';
				}
			},this)
		}
		
,	loadClassifaction: function(url){
		var serviceName = Ext.decode(localStorage.getItem("settings")) || Config;
		var newService = serviceName["general"]["nfServer"];
			if (Ext.isEmpty(url)) {
				TaxonTagger.Notice.msg(this.txtNoticeTitle, this.txtemptyText);
			}else if (!this.isURL(url)) {
				TaxonTagger.Notice.msg(this.txtNoticeTitle, this.txtUrlText);
			} else if(this.lasturlLoaded == url && this.lastService == newService){
				TaxonTagger.Notice.msg(this.txtNoticeTitle, this.msgDocumentAlreadyLoaded);
			} else {
				this.htw.store.removeAll();
				this.htw.loadedList = false;
				this.htw.getBottomToolbar().clearStatus();
				this.htw.getBottomToolbar().setText(this.htw.bbarTxt);
				this.loadStore(url, newService);
				this.lasturlLoaded = url;
				this.lastService = newService;
			}
	}
,	isURL: function(s){
			var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
			return regexp.test(s);
	}
,	loadStore: function(url, newService){
		Ext.Ajax.request({
					url: Config.baseUrl + 'resources/api/transform.php'
				,	method: 'POST'
				,	scope: this
				,	params: {
							service: newService
						,	url: encodeURI(url)
						,	output: 'json'
					}
				,	success: function(responseObject){
						var o = Ext.decode(responseObject.responseText);
						if(!Ext.isEmpty(o.names)){
							TaxonTagger.docStore.loadData(o.names, true);
							TaxonTagger.loadStorage();
						}	
						this.htw.loadMask.hide();
					}
				,	failure: function(res){
						TaxonTagger.Notice.msg("Error", "Fatal Error.");
						this.htw.loadMask.hide();
					}
			}, this);
	}
,	loadNames: function(namesStr){
		var namesArray = namesStr.split('\n');
		for(var i=0; i<namesArray.length; i++){
			if(!Ext.isEmpty(namesArray[i])){
				var index = TaxonTagger.docStore.findExact('scientificName', namesArray[i]);
				if(index == -1){
					this.addRecord(namesArray[i]);	
					if(TaxonTagger.docStore.getCount() > 0){
						this.tbar.enableChild();
					}
					this.tbar.clearEnableDisable();
					this.htw.newAdded = true;
				}	
			}
		}
	}
,	addRecord: function(name){
		var createRecord = Ext.data.Record.create([
				{name: "scientificName", type: "string"}
			,	{name: "orig", type: "string"}
		]);
		var record = new createRecord({
				scientificName: name
			,	orig: name
		});
		TaxonTagger.docStore.add(record);
		TaxonTagger.docStore.commitChanges();
	}	
});